# python -m project_root.api.api_site
from .api_token import Token 
from util.time_util import timestran
import requests

class Api_Site(Token):


    def get_topdata(self):
        url = 'http://hxzhan.rtcloud.com:32111/tm/station/head'
        json = {
            'scene_id':137,
        }
        keys = ['gateway_count','device_count','alarm_device_count','max_env_temp','max_env_humid']
        response = requests.post(url=url,headers=self.get_header(),json=json)
        values = []
        data = response.json()['data']
        for key in keys:
            if data[key] != '-':
                values.append(data[key])
        return values

    def get_alarmlist(self):
        try:
            url = 'http://hxzhan.rtcloud.com:32111/tm/alarm/list'
            json = {
                'limit':6,
                'page':1,
                'scene_id':137
            }
            keys = ['alarm_at','device_name','desc','level','return_status']
            response = requests.post(url=url,headers=self.get_header(),json=json)
            assert response.status_code == 200
            self.logger.info("调用接口 get_alarmlist 成功 ")
            values = []
            for data in response.json()['data']['list']:
                for key in keys:
                    if key == 'alarm_at':
                        values.append(timestran(data[key]))
                    elif key == 'level':
                        if data[key] == 1:
                            values.append('预警')
                        elif data[key] == 2:
                            values.append('报警')
                    elif key == 'return_status':
                        if data[key] == 1:
                            values.append('已复归')
                        elif data[key] == 2:
                            values.append('未复归')
                    else:
                        values.append(data[key])
            return values
        except Exception as e:
            self.logger.error("调用接口 get_alarmlist 失败",e)
        
    def get_devicelist(self):
        values = []
        url = 'http://hxzhan.rtcloud.com:32111/tm/station/device/realtime'
        json = {
            'limit':12,
            'page':1,
            'scene_id':137
        }
        response = requests.post(url=url,headers=self.get_header(),json=json)
        keys = ['name','online_status','highest_temp','lowest_temp','refresh_at']
        for data in response.json()['data']['list']:
            for key in keys:
                if key == 'online_status':
                    if data[key] == 1:
                        values.append('在线')
                    elif data[key] == 2:
                        values.append('离线')
                elif key == 'highest_temp' or key == 'lowest_temp':
                    if data[key] == 0:
                        values.append('-')
                    else:
                        values.append(str(data[key]))
                elif key == 'refresh_at':
                    if data[key] == 0:
                        values.append('-')
                    else:
                        values.append(timestran(data[key]))
                else:
                    values.append(data[key])
        return values

    def get_devicetable(self):
        url = 'http://hxzhan.rtcloud.com:32111/tm/station/device/realtime/table'
        json = {
            'limit':20,
            'page':1,
            'scene_id':137
        }
        response = requests.post(url=url,headers=self.get_header(),json=json)
        values = []
        keys = ['name','mp_name','inner_region_name','refresh_at','val']
        for data in response.json()['data']['list']:
            for key in keys:
                if key == 'refresh_at':
                    if data[key] == 0:
                        values.append('-')
                    else:
                        values.append(timestran(data[key]))
                elif key == 'val':
                    if data[key] == 0:
                        values.append('-')
                    else:
                        values.append(str(data[key]))
                else:
                    values.append(data[key])
        return values

# test = Api_Site()
# # print(test.get_devicetable())